<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      单价：{{unitprice}} <br />
      数量：<counter-demo @qtychange="handle"></counter-demo>
      总价：{{total}}
    </div>
    <script src="../lib/vue.js"></script>
    <script>
      Vue.component('counter-demo', {
        data: function () {
          return {
            count: 0,
          }
        },
        template: `
          <div>
            <input type="button" value="+" @click="jia">
            <span>{{count}}</span>
            <input type="button" value="-" @click="jian">
          </div>
        `,
        methods: {
          jia: function () {
            this.count++
            this.$emit('qtychange', this.count)
          },
          jian: function () {
            this.count--
            this.$emit('qtychange', this.count)
          },
        },
      })

      new Vue({
        el: '#app',
        data: {
          unitprice: 10,
          qty: 0,
        },
        computed: {
          total: function () {
            return this.unitprice * this.qty
          },
        },
        methods: {
          handle: function (qty) {
            console.log('接收数量变化')
            this.qty = qty
          },
        },
      })
    </script>
  </body>
</html>
